import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_base/im_base.dart';
import 'package:im_base/public/components/avatar_widgets.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info.dart';

import 'logic.dart';

///黑名单列表功能
class BlackListWidget extends StatelessWidget {
  final logic = Get.put(BlackListLogic());

  ///将点击事件抛出
  final Function(V2TimFriendInfo) itemClick;

  BlackListWidget(this.itemClick);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<BlackListLogic>(
      init: logic,
      builder: (controller) {
        return LoadingWidget(
            status: logic.status,
            child: GetBuilder<BlackListLogic>(
              init: logic,
              id: 'list',
              builder: (controller) {
                return ListView.builder(
                    itemCount: controller.blackList.length,
                    itemBuilder: (context, index) {
                      if (controller.hasMore &&
                          index == controller.blackList.length - 2) {
                        controller.loadMore();
                      }
                      return itemView(controller.blackList[index]);
                    });
              },
            ));
      },
    );
  }

  Widget itemView(V2TimFriendInfo info) {
    return GestureDetector(
      onTap: () => itemClick(info),
      child: Container(
        padding: EdgeInsets.fromLTRB(16, 8, 16, 8),
        decoration: BoxDecoration(
            border: Border(bottom: BorderSide(color: Colors.grey[300]!))),
        child: Row(
          children: [
            Avatar(
              backgroundImage: NetworkImage(info.userProfile?.faceUrl ?? ''),
              shape: AvatarShape.circle,
              size: AvatarSize.MEDIUM,
            ),
            Expanded(
                child: Text(info.userProfile?.nickName ?? info.userID)
                    .marginOnly(left: 8)),
            TextButton(
                onPressed: () => logic.removeBlack(info),
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.blue),
                    padding: MaterialStateProperty.all(EdgeInsets.zero),
                    minimumSize: MaterialStateProperty.all(Size(60, 30)),
                    tapTargetSize: MaterialTapTargetSize.shrinkWrap),
                child: Text(
                  '移除',
                  style: TextStyle(fontSize: 14, color: Colors.white),
                ))
          ],
        ),
      ),
    );
  }
}
